// 合成图文部分
define( [
  'jquery',
  'require',
  'bigwe_component',
  '../lib/editData',
  '../modules/autoSave',
  '../lib/utils',
  'message',
  'articles_choose',
  '../lib/dataHandle',
  'bigwe_utils',
  '../modules/createEditor',
  '../lib/dataHandle',
  'header',
  'bigwe_component'
], function( $, require ) {
  var bc = require( 'bigwe_component' );
  var dh = require( '../lib/dataHandle' );
  var ac = require( 'articles_choose' );
  var header = require( 'header' );
  var editData = require( '../lib/editData' );
  var AutoSave = require( '../modules/autoSave' );
  var GraphBuild = require( '../lib/utils' );
  var ShowTitleMessage = require( 'message' );
  var editorObj = require( '../modules/createEditor' );
  var articleBean = require( '../lib/dataHandle' ).articleBean;

  var components = require( 'bigwe_component' );

  var uts = GraphBuild;
  var showTime = ShowTitleMessage.showTime;
  var $graphManage = $( '.graph-manage' );

  var thisUtils = {
    // 交换数据位置
    orderExchange: function( order, obj ) {
      var graphData = editData.graphDataDetails;
      var thisData = graphData[ obj.index() ];
      graphData[ obj.index() ] = graphData[ obj.index() + order ];
      graphData[ obj.index() + order ] = thisData;
      AutoSave.start();
    },
    // 改变当前编辑的index
    editIndexChange: function() {
      editData.editIndex = $( '.selected-li', $graphManage ).index();
    }

  };

  // 选择文章
  ac( {
    userTag: $( '#userTag' ).val()
  }, function( articleID, articleHTML ) {
    ShowTitleMessage.show( "正在拼命拉取数据" );
    uts.getArticleByIdAjax( articleID, function( data ) {
      var jsonObj = data;
      delete jsonObj.article_id;

      function createArt() {
        GraphBuild.graphListCreate( jsonObj ).addClass( 'selected-li' );
        GraphBuild.htmlLoadIn( jsonObj );
      }
      if ( editData.editType == 1 ) {
        if ( !$.trim( editorObj.getContent() ) ) {
          createArt();
        } else {
          components.confirm( createArt );
        }

      } else {
        $( '.js_writing', GraphBuild.graphListCreate( jsonObj, editData.graphDataDetails.length ) ).trigger( 'click' );
      }
      ShowTitleMessage.hide();
    } );
  } );

  $( '[data-toggle="tooltip"]', $graphManage ).tooltip( {
    container: 'body',
    trigger: 'hover'
  } );

  $graphManage.on( 'click', '.comb-list [delete-event="comb"]', function( e ) {
    e.stopPropagation();
    var $_this = $( this );
    var $liThis = $_this.parents( '.graph_item' );
    bc.deleteConfirmShowBottom( {
      obj: $_this,
      fun: function() {
        var prevIndex = $liThis.prev().index();
        var nextIndex = $liThis.next().index();
        var thisIndex = $liThis.index();

        editData.isContentChange = true;

        var currentEdit = prevIndex < 0 ? thisIndex : prevIndex;

        if ( thisIndex != 0 || ( thisIndex == 0 && prevIndex > 0 ) || ( thisIndex == 0 && nextIndex > 0 ) ) {

          editData.graphDataDetails.splice( thisIndex, 1 );
          $liThis.remove();

          if ( thisIndex == editData.editIndex || ( thisIndex == 0 && nextIndex > -1 && thisIndex == editData.editIndex ) ) {
            $( '.comb-list>li', $graphManage ).eq( currentEdit ).find( '.js_writing' ).trigger( 'click' );
          } else {
            editData.editIndex = $( '.comb-list>li.selected-li', $graphManage ).index();
          }

        } else {
          editData.editIndex = 0;
          editData.graphDataDetails[ 0 ] = new articleBean();
          GraphBuild.htmlLoadIn( null );
          $( '.title', $liThis ).text( '' );
          $( '.cover', $liThis ).css( 'background-image', '' );
        }

      }
    } );
  } ).on( "click", "#select_frommine", function() {
    $( "#util_article_choose" ).modal( "show" );
  } ).on( 'click', '.js_writing', function( e ) { // 编辑图文
    e.stopPropagation();
    var $parentLi = $( this ).parents( '.graph_item' );
    var $thisGraph = editData.graphDataDetails;
    var $index = $parentLi.index();
    $parentLi.addClass( 'selected-li' ).siblings().removeClass( 'selected-li' );
    var dataObj = $thisGraph[ $index ];
    editData.editIndex = $index;
    GraphBuild.htmlLoadIn( dataObj );
  } ).on( 'click', '.comb-list .comb-detail-div', function() { // 增大编辑图文可点击区域
    $( '.js_writing', this.parentElement ).trigger( 'click' );
  } ).on( 'click', '.comb-list .icon-iconfontmovetodown', function( e ) { // 往下移动图文
    e.stopPropagation();
    var $_parent = $( this ).parents( '.graph_item' );
    var $parentNextHeight = $_parent.next().outerHeight();
    var $parentHeight = $_parent.outerHeight();

    thisUtils.orderExchange( 1, $_parent );
    $_parent.next().transition( {
      y: -$_parent.outerHeight()
    }, function() {
      $( this ).css( 'transform', 'none' );
    } );
    $_parent.transition( {
      y: $_parent.next().outerHeight()
    }, function() {
      $_parent.next().after( $( this ).clone().css( {
        'transform': 'none',
        'animation': 'none'
      } ) );
      $( '[data-toggle="tooltip"]', $graphManage ).tooltip( {
        container: 'body'
      } );
      $( '.tooltip[role="tooltip"]' ).remove();
      $( this ).remove();
      thisUtils.editIndexChange();
    } );
    $( '[data-toggle="tooltip"]', $graphManage ).tooltip( 'hide' );
  } ).on( 'click', '.comb-list .icon-iconfontmovetoup', function( e ) { // 移动顺序 向上
    e.stopPropagation();
    var $_parent = $( this ).parents( '.graph_item' );
    var $parentNextHeight = $_parent.prev().outerHeight();
    var $parentHeight = $_parent.outerHeight();
    thisUtils.orderExchange( -1, $_parent );

    $_parent.prev().transition( {
      y: $_parent.outerHeight()
    }, function() {
      $( this ).css( 'transform', 'none' );
    } );
    $_parent.transition( {
      y: -$_parent.prev().outerHeight()
    }, function() {
      $_parent.prev().before( $( this ).clone().css( {
        'transform': 'none',
        'animation': 'none'
      } ) );

      $( '[data-toggle="tooltip"]', $graphManage ).tooltip( {
        container: 'body'
      } );
      $( '.tooltip[role="tooltip"]' ).remove();
      $( this ).remove();
      thisUtils.editIndexChange();
    } )
  } ).on( "click", "#btn_creat_graph", function() { // 新建图文
    $( '.js_writing', uts.graphListCreate( new dh.articleBean(), editData.graphDataDetails.length ) ).trigger( 'click' );
  } );

  // 右侧图文
  var graphManMaxHeight = $( window ).height() - header.$selector.outerHeight() - $( '.mpnews_title', $graphManage ).outerHeight() - $( '.fixed-save-sycn' ).outerHeight() - $( '.comb-addmore', $graphManage ).outerHeight() - 70 /* 间隙 */ ;
  $( '.edit-column3' )
    /*.keepOn( {
    		datumTop: header.$selector.outerHeight(),
    		autoFullPlace: false
    	} )*/
    .find( '.comb-list' ).css( {
      maxHeight: graphManMaxHeight
    } );

  var Graph = {
    topAndBottomMove: function() {

      return this;
    },
    chooseArticle: function() {
      return this;
    },
    // 编辑图文
    editGrap: function() {
      var $this = this;
      return this;
    },
    AGFunArray: [ // 废弃的
      function() {
        // 加载图文
        var articleGroup = bu.getUrlParam( 'article_group' );
        // 判断是否为编辑图文 再去请求图文
        if ( articleGroup ) {
          ShowTitleMessage.show( '正在载入图文数据' );
          $.get( 'edit_text/get_edit_chart', {
            'article_group': articleGroup
          }, function( data ) {
            ShowTitleMessage.hide();
            var jsonObj = $.parseJSON( data );
            if ( data === '[]' || !data || jsonObj.error ) {
              ShowTitleMessage.showWarning( jsonObj.msg || '图文数据出现错误，当前为新建图文模式', showTime );
              return;
            }
            // 判断是否有草稿
            //AutoSave.showDraftLink();

            editData.isCreate = false;
            editData.articleGroup = articleGroup;

            if ( jsonObj.length == 0 ) return;
            GraphBuild.graghListCreateJsons( jsonObj, true );
            editData.ueeditorAndEditDataIsReady = !0;
            editData.allIsReady++;
          } );
        } else {
          // 判断是否有草稿
          //AutoSave.showDraftLink();
        }
      },
      function() {
        var articleID = bu.getUrlParam( 'cd' );
        if ( articleID ) {
          ShowTitleMessage.show( '正在载入文章数据' );
          $.get( '/article/get_art_info', {
            "cd": articleID
          }, function( data ) {
            editData.allIsReady++;
            ShowTitleMessage.hide();
            var noData = '文章数据发生错误！当前为新建文章模式';
            if ( !data.data.id ) {
              ShowTitleMessage.showWarning( noData, showTime );
              return;
            }
            if ( data.ret ) {
              ShowTitleMessage.showWarning( data.msg || noData, showTime );
              return;
            }
            if ( data.data.original_author ) {
              data.data.declare_original = 1;
            }
            data = articleObjTransition( data.data, true );

            if ( data.authorName ) {
              data.declare_original = 1;
            }

            editData.isCreate = false;
            GraphBuild.graghListCreateJsons( [ data ], true );

            editData.articleID = articleID;
            editData.ueeditorAndEditDataIsReady = !0;
          } );
        }
      }
    ]
  };

} );